package com.west_traveler.ximalayafm;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.test.mock.MockApplication;
import android.view.View;
import android.widget.ImageView;

import com.west_traveler.application.BaseApplication;
import com.west_traveler.entity.entity_live_page.entity_live_second_page.Province;
import com.west_traveler.entity.entity_live_page.entity_live_second_page.ProvinceRadio;
import com.west_traveler.find_page_fragment.find_live_provinceradio_frg.ProvinceRadioFrg;
import com.west_traveler.viewpager_adapter.ProvinceRadioFrgAdapter;

import java.util.LinkedList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ProvinceRadioActivity extends AppCompatActivity {

    private ImageView ivBack;
    private TabLayout tabLayout;
    private ImageView ivArrawDown;
    private ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_province_radio);
        //初始化控件
        initViews();
        //关于导航栏和viewPager
        aboutViewPager();

    }

    //关于导航栏和viewPager
    private void aboutViewPager() {
        //设置viewpager和tablayout关联
        tabLayout.setupWithViewPager(viewPager);
        //获取省市列表
        ((BaseApplication) getApplication()).getLiveHttpService()
                .getProvinceList().enqueue(new Callback<ProvinceRadio>() {
            @Override
            public void onResponse(Call<ProvinceRadio> call, Response<ProvinceRadio> response) {
               //数据源
                List<Province> provinceList = response.body().getResult();
                LinkedList<String> provinceNames = new LinkedList<>();
                for (int i = 0; i < provinceList.size(); i++) {
                    provinceNames.add(provinceList.get(i).getProvinceName());
                }
                LinkedList<Fragment> frgs = new LinkedList<>();
                for (int i = 0; i < provinceNames.size(); i++) {
                    frgs.add(new ProvinceRadioFrg(provinceList.get(i).getProvinceCode()));
                }
                //适配器
                ProvinceRadioFrgAdapter adapter = new ProvinceRadioFrgAdapter(getSupportFragmentManager(), provinceNames, frgs);

                //绑定适配器
                viewPager.setAdapter(adapter);
            }

            @Override
            public void onFailure(Call<ProvinceRadio> call, Throwable t) {

            }
        });
    }

    //初始化控件
    private void initViews() {
        ivBack = ((ImageView) findViewById(R.id.iv_radio_provincepageback));
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
                overridePendingTransition(android.R.anim.fade_in, R.anim.push_left_out);
            }
        });
        tabLayout = ((TabLayout) findViewById(R.id.tab_province_radio));
        ivArrawDown = ((ImageView) findViewById(R.id.iv_provinceradio_arraw_down));
        viewPager = ((ViewPager) findViewById(R.id.viewpager_live_provincepagesecond_id));
    }
}
